import datetime
from datetime import timedelta
import akshare as ak
import pandas as pd
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse


def ZhangTingHuoSiTian(symbol,date):
    start_date = PanDuanstartdate(date)
    end_date = JiSuanenddate(start_date)
    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=symbol,
                                            period="daily",
                                            start_date=start_date,
                                            end_date=end_date,
                                            adjust="qfq")
    stock_zh_a_hist_dict=stock_zh_a_hist_df.to_dict()
    print(stock_zh_a_hist_dict)
#     第一天
    try:
        zhangtingjia=stock_zh_a_hist_dict["收盘"][0]
        day_1_ZuiGao=stock_zh_a_hist_dict["最高"][1]-0.1
        if day_1_ZuiGao > zhangtingjia or day_1_ZuiGao==zhangtingjia:
            day_1="+"
        else:
            day_1="-"
    #     第二天
        day_1_shoupanjia=stock_zh_a_hist_dict["收盘"][1]
        day_2_ZuiGao=stock_zh_a_hist_dict["最高"][2]-0.1
        if day_2_ZuiGao > day_1_shoupanjia or day_2_ZuiGao == day_1_shoupanjia:
            day_2="+"
        else:
            day_2="-"
    #     第三天
        day_2_shoupanjia=stock_zh_a_hist_dict["收盘"][2]
        day_3_ZuiGao=stock_zh_a_hist_dict["最高"][3]-0.1
        if day_3_ZuiGao > day_2_shoupanjia or day_3_ZuiGao == day_2_shoupanjia:
            day_3="+"
        else:
            day_3="-"
    #     第四天
        day_3_shoupanjia=stock_zh_a_hist_dict["收盘"][3]
        day_4_ZuiGao=stock_zh_a_hist_dict["最高"][4]-0.1
        if day_4_ZuiGao > day_3_shoupanjia or day_4_ZuiGao == day_3_shoupanjia:
            day_4="+"
        else:
            day_4="-"
        day_zt={
            "day_1":day_1,
            "day_2":day_2,
            "day_3":day_3,
            "day_4":day_4,
        }
    except:
        day_zt={
            "day_1":"/",
            "day_2":"/",
            "day_3":"/",
            "day_4":"/",
        }
    return day_zt
def JiSuanenddate(start_date):
    year = start_date[0:4]
    month = start_date[4:6]
    day = start_date[6:8]
    input_date = f"{year}-{month}-{day}"
    date_obj = datetime.datetime.strptime(input_date, "%Y-%m-%d")
    # 使用date.weekday()方法获取星期几(0-6，0表示星期一)
    weekday = date_obj.weekday()
    print(date_obj)
    if weekday == 0:
        next_day = date_obj + timedelta(days=4)
    if weekday in [1,2,3,4]:
        next_day = date_obj + timedelta(days=6)
    next_day = str(next_day).split(' ')[0].split('-')
    end_date =''.join(next_day)
    return  end_date

def PanDuanstartdate(start_date):
    year = start_date[0:4]
    month = start_date[4:6]
    day = start_date[6:8]
    input_date = f"{year}-{month}-{day}"
    date_obj = datetime.datetime.strptime(input_date, "%Y-%m-%d")
    # 使用date.weekday()方法获取星期几(0-6，0表示星期一)
    weekday = date_obj.weekday()
    print(date_obj)
    if weekday == 5:
        next_day = date_obj - timedelta(days=1)
    elif weekday == 6:
        next_day = date_obj - timedelta(days=2)
    else:
        next_day = date_obj
    next_day = str(next_day).split(' ')[0].split('-')
    start_date =''.join(next_day)
    return start_date

if __name__ == '__main__':
    symbol="000595"
    date="20240729"
    day_zt=ZhangTingHuoSiTian(symbol,date)
    print(day_zt)
